package com.lifa.controller;

import com.lifa.dao.*;
import com.lifa.dao.RolePermissionDao;
import com.lifa.entity.Permission;
import com.lifa.entity.Role;
import com.lifa.pojo.PermissionBean;
import com.lifa.pojo.StateBean;
import com.lifa.service.RoleService;
import com.lifa.dao.PermissionDao;
import com.lifa.dao.RoleDao;
import com.lifa.entity.Role;
import com.lifa.pojo.PermissionBean;
import com.lifa.pojo.StateBean;
import com.lifa.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@Scope("prototype")
public class RoleController {
    @Autowired
    private RoleService roleService ;
    @Autowired
    private RoleDao roleDao ;
    @Autowired
    private PermissionDao permissionDao ;
    @Autowired
    private RolePermissionDao rolePermissionDao ;
    @RequestMapping("/doFindAll")
    @ResponseBody
    public Map<String,Object> doFindAll(@RequestParam(value = "rname",required = false,defaultValue = "") String rname, int limit,int offset){
        Map<String,Object> map = new HashMap<>() ;
        map.put("total",roleService.findCount(rname)) ;
        map.put("rows",roleService.findAll(rname,limit,offset)) ;
        return map ;
    }
@RequestMapping("/query")
@ResponseBody
    public List<Role> query(int id){
        //查询当前用户所有拥有的角色
    Role role = roleDao.getRoleByUserId(id) ;
    //查询所有角色
    List<Role> list = roleDao.queryAll() ;
    for(Role r:list){
        if(role.getId() == r.getId()){
            r.setIschecked(true);
        }
    }
        return list ;
    }
    @RequestMapping("/doFenPen")
    @ResponseBody
    public List<PermissionBean> doFenPei(int roleid){
        //得到当前角色拥有的所有权限
        List<Permission> listAll = rolePermissionDao.queryByRoleId(roleid) ;
        //获取根节点
        List<PermissionBean> list = permissionDao.query(0) ;
        for(PermissionBean permissionBean :list){
            int id = permissionBean.getId() ;
            //查询子节点
            List<PermissionBean> list1 = permissionDao.query(id) ;
            permissionBean.setNodes(list1);
        }
        //回显当前角色拥有的所有权限
        for(PermissionBean per : list){
            StateBean stateBean = new StateBean() ;
            for(Permission permission : listAll){
                if(per.getId() == permission.getId()){
                    stateBean.setChecked(true);
                }
            }
            per.setState(stateBean);
            //所有的子节点
            List<PermissionBean> listSon = per.getNodes() ;
            for(PermissionBean permission1 : listSon){
                StateBean stateBean1 = new StateBean() ;
                for(Permission permission11 : listAll){
                    if(permission1.getId() == permission11.getId()){
                        stateBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                permission1.setState(stateBean1);
            }
        }



        return list ;
    }
}
